package serverDB_classes;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class Main {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		SessionFactory sessionFactory=new Configuration().configure().buildSessionFactory();
		Session session=sessionFactory.openSession();
		
		UserData user;
		Dir dir;	
		
		for (String string : DBManager.getDBManager().getRelativeFiles("yoavip@hotmail.com", "yoav")){
			System.out.println(string);
		}
		//System.out.println(changedDir.getDirPath());
		//DBManager.getDBManager().newUser("testing", "1234", "te", "st");
		//DBManager.getDBManager().deleteDir(dirPath, userName)dedeleteCheckSum("8c65be4958d4476ab3dae0e51975521c");//newFile("0000", "mytest\\file1.txt", "testing");//.newDir("testing", "mytest");
		//session.beginTransaction();
		/*try{
			int index=0;
			
			for(;;){
				index++;

				//DBManager.getDBManager().isFileExists(index);
				
				
			}
			/*
			
			/*
			UserFiles file=new UserFiles();
			file.setFilePath("\\New Text Document (33).txt");
			file.setCheckSum(findCheckSum(session, 0));
			dir=getDirOfFile(session, "yoavip@hotmail.com", "\\");
			user=getUser(session, "yoavip@hotmail.com");
			session.save(file);
			
			dir.getFiles().add(file);
			session.update(dir);
		
			session.getTransaction().commit();
			*/	/*	
		}catch (Exception e){
			System.out.println(e.getMessage());
			e.printStackTrace();
			//session.getTransaction().rollback();
		}*/
		

		//DBManager.getDBManager().newUser("yoavytryrt@walla.com", "hey", "yoav", "ma", "/");
		
		System.out.println("done");

		/*
		String findDir="From UserData Where userName='yoavip@hotmail.com'";
		Query query=session.createQuery(findDir);		
		List <UserData> users=(List<UserData>)query.list();
		for (Dir dir: users.get(0).getDirs()){
			if (dir.getDirPath().equals("C:"+System.getProperty("file.separator")+"DropBox")){
				System.out.println("found it!");
			}
		}
		*/	

	}

	public static Dir getDirOfFile(Session session,String userName, String dirPath) {
		String findDir="From UserData Where userName='"+userName+"'";
		Query query=session.createQuery(findDir);		
		List <UserData> users=(List<UserData>)query.list();
		for (Dir dir: users.get(0).getDirs()){
			if (dir.getDirPath().equals(dirPath)){
				return dir;
			}
		}	
		return null;
	}
	public static CheckSums findCheckSum(Session session,long checkSum){
		String hql = "From CheckSums checksum Where checksum.checkSum=:check";
		Query query = session.createQuery(hql).setLong("check", checkSum);
		if (query.list().size()>0){
			return (CheckSums)query.list().get(0);
		}
		return null;
		
	}
	public static UserData getUser(Session session,String userName){		
		String getUserName="From UserData user Where user.userName=:userName";
		Query query=session.createQuery(getUserName).setParameter("userName",userName);
		if (query.list().size()==1){
			return(UserData) query.list().get(0);
		}
		return null;
	}
}
